/*
 * Interruptverwaltung.h
 *
 *  Created on: 03.05.2011
 *      Author: Jasper
 */

#ifndef INTERRUPTVERWALTUNG_H_
#define INTERRUPTVERWALTUNG_H_

#include "constants.h"
#include <sys/neutrino.h>
#include <stdlib.h>
#include "HWaccess.h"
#include <pthread.h>
#include <semaphore.h>
#include <malloc.h>
#include <errno.h>
#include <sys/mman.h>
#include <process.h>

#ifdef SIMULATION
#include <ioaccess.h>
#endif

#define LISTSIZE 12
#define ISR_MESSAGE_CODE 10

struct listItem
{
    struct listItem* next;
    int channelID;
    int connectionID;
    int pin;
    char messageCode;
    int messageContent;
    pid_t pid;
};


class Interruptverwaltung
{

private:
    struct listItem * list[LISTSIZE]; // contains Listheads. list[0] to list[7] PORTB; list[8] to list[11] PORTC
    int lastValB;
    int lastValC;
    volatile int irqStatus;
    volatile bool running;
    pthread_t pthread;
    pthread_mutex_t lockLists;
    sem_t waitForInit;

    static pthread_mutex_t lockSingleton;
    static Interruptverwaltung * instance;

    int init();
    Interruptverwaltung();
    virtual ~Interruptverwaltung();
    static const struct sigevent * InterruptverwaltungISR(void *arg, int id);
    static void * InterruptverwaltungManagementThreadRoutine(void * arg);
    static void InterruptverwaltungSendPulses(struct listItem * first);

public:
    int addListener(int channelID, pid_t pid, int pin, char messageCode, int messageContent);
    int removeListener(int channelID, pid_t pid, int pin);
    static Interruptverwaltung * getInstance();



};

#endif /* INTERRUPTVERWALTUNG_H_ */
